package com.yoomiito.app.widget.dialog

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.alibaba.fastjson.JSONObject
import com.yoomiito.app.R
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.comment.Comment
import com.yoomiito.app.model.comment.CommentInfo
import com.yoomiito.app.net.NetErrorCode
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.small_tip_1.*
import org.greenrobot.eventbus.EventBus

class InputCommentDialog(val context: BaseActivity<*>, val id: String, val type: String,val nickName:String="") : BaseDialog(context, R.style.dialog_1) {
    private lateinit var inputEt: EditText
    var onMessageListener: (Comment,Boolean) -> Unit = { _,_ -> }
    override fun setView(): View {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_input_comment, null, false)
        inputEt = view.findViewById(R.id.inputEt)
        val drawable = inputEt.background as GradientDrawable
        drawable.cornerRadius = DensityUtil.dp2px(16f).toFloat()
        if (nickName.isNullOrEmpty()){
            inputEt.hint = "说点什么吧～"
        }else{
            inputEt.hint = "回复 @$nickName"
        }
        val posMsg = view.findViewById<TextView>(R.id.postMsg)
        val drawable_1 = posMsg.background as GradientDrawable
        drawable_1.cornerRadius = DensityUtil.dp2px(16f).toFloat()
        posMsg.setOnClickListener {
            val content = inputEt.text.toString()
            if (content.isNullOrEmpty() || content.isNullOrBlank()) {
                "输入内容不能为空".showToast()
            } else {
                comment(id, content, type)
            }
        }
        setmGravity(Gravity.BOTTOM)
        setWidthPercent(1.toDouble())
        setmHeightPercent((-1).toDouble())
        setCanceledOnTouchOutside(true)
        return view
    }

    override fun show() {
        super.show()
        inputEt.isFocusableInTouchMode = true
        inputEt.requestFocus()
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
    }

    private fun comment(id: String, content: String, type: String) {
        context.showLoading()
        YmApi.getYmService()
                .postComment(id, content, type)
                .compose(context.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<CommentInfo>() {
                    override fun onSuccess(t: CommentInfo?) {
                        context.dismissLoading()
                        val comment = Comment()
                        comment.id = t?.commentId
                        comment.nickName = t?.nickName
                        comment.isReplay = t?.isReplay
                        comment.replayName = t?.replayName
                        comment.time = t?.time
                        comment.likeNumber = t?.likeNumber
                        comment.commentContent = t?.commentContent
                        comment.headUrl = t?.headUrl
                        comment.type = if (type == "1") 1 else 2
                        onMessageListener(comment,false)
                        dismiss()
                    }

                    override fun onFail(error: ApiException?) {
                        context.dismissLoading()
                        if (error?.code == NetErrorCode.NOTENOUGHLEVEL) {
                            val tipDialog = TipDialog(context)
                            tipDialog.setContent("评论功能暂时只对VIP开放")
                                    .setCancelText("关闭")
                                    .setSureText("成为VIP")
                                    .setTitle("温馨提示")
                                    .setClickListener {
                                        dismiss()
                                        tipDialog.dismiss()
                                        onMessageListener(Comment(),true)
                                        context.finish()
                                        RouterUtils.toMain(context, 1)
                                    }
                                    .show()
                        } else {
                            "${error?.msg}${if (NetErrorCode.DISABLEWORD == error?.code) " 涉及敏感词" else ""}".showToast()
                        }
                    }

                })
    }
}